package com.lhkj.ct.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.admin.model.entity.TblDictType;
import com.lhkj.ct.meta.admin.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *      字典类型
 * </p>
 */
@RestController
@RequestMapping("api/system/dict/type")
@ApiSupport(order = 8)
@Api(tags = {"08.字典类型"})
//@RequiresRoles(value = {SystemConstants.ADMIN_ROLE_CODE})
public class SysDictTypeController {

    @Resource
    private SysDictTypeService sysDictTypeService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "字典列表")
    @GetMapping("/list")
    ////@RequiresPermissions("system:dictType:list")
    public ActionResult<IPage<TblDictType>> list(TblDictType dictType, PageQuery pageQuery) {
        return ActionResult.success(sysDictTypeService.selectDictTypePage(dictType, pageQuery));
    }

//    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
//    ////@RequiresPermissions("system:dict:export")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysDictType dictType)
//    {
//        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
//        util.exportExcel(response, list, "字典类型");
//    }

    /**
     * 查询字典类型详细
     */
    ////@RequiresPermissions("system:dictType:get")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取表单数据")
    @GetMapping(value = "/{dictId}")
    public ActionResult<TblDictType> getInfo(@PathVariable String dictId) {
        return ActionResult.success(sysDictTypeService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    ////@RequiresPermissions("system:dictType:add")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增")
    @Log(title = "字典类型", actionType = ActionType.INSERT)
    @PostMapping
    public ActionResult<Void> add(@Validated @RequestBody TblDictType dict) {
        if (!sysDictTypeService.checkTypeCodeUnique(dict)) {
            return ActionResult.fail("新增字典'" + dict.getName() + "'失败，字典编码已存在");
        }
        return ActionResult.judge(sysDictTypeService.insert(dict));
    }

    /**
     * 修改字典类型
     */
    ////@RequiresPermissions("system:dict:edit")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "编辑")
    @Log(title = "字典类型", actionType = ActionType.UPDATE)
    @PutMapping
    public ActionResult<Void> edit(@Validated(value = {ValGroups.Update.class}) @RequestBody TblDictType dict) {
        TblDictType target = sysDictTypeService.getById(dict.getId());
        if (null == target) {
            return ActionResult.fail("字典不存在或字典id无效");
        }
        if (!sysDictTypeService.checkTypeCodeUnique(dict)) {
            return ActionResult.fail(String.format("修改字典[%s]失败，字典编码已存在", dict.getName()));
        }
        return ActionResult.judge(sysDictTypeService.updateById(dict));
    }

    /**
     * 删除字典类型
     */
    ////@RequiresPermissions("system:dict:remove")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除-支持批量")
    @Log(title = "字典类型", actionType = ActionType.DELETE)
    @DeleteMapping("delete")
    public ActionResult<Void> remove(@RequestBody @NotEmpty(message = "删除数据不能为空") List<String> dictTypeIds) {
        sysDictTypeService.removeByIds(dictTypeIds);
        return ActionResult.success();
    }

    /**
     * 刷新字典缓存
     */
    ////@RequiresPermissions("system:dict:remove")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "刷新字典缓存")
    @Log(title = "字典类型", actionType = ActionType.INSERT)
    @DeleteMapping("/refreshCache")
    public ActionResult<Void> refreshCache() {
        sysDictTypeService.resetDictCache();
        return ActionResult.success();
    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取字典类型选择框列表")
    @GetMapping("/options")
    public ActionResult<List<TblDictType>> options() {
        List<TblDictType> dictTypes = sysDictTypeService.options();
        return ActionResult.success(dictTypes);
    }
}
